package org.ns.summer.classpath.scan;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ns.summer.classpath.classreader.ClassFile;
import org.ns.summer.classpath.classreader.ClassReader;
import org.ns.summer.classpath.classreader.entity.ClassInfo;
import org.ns.summer.classpath.scan.io.ScanFileInputStream;
/**
 * @author 张星宇
 *
 * @email m.vip@qq.com
 */
public class ScanFileUtils {

	public static File getFile(String path) {
		return new File(getFileString(path));
	}

	public static String getFilePathString(String path) {
		int filePathIndex = path.lastIndexOf("!");
		if (filePathIndex != -1) {
			return path.substring(filePathIndex + 1);
		}
		filePathIndex = path.lastIndexOf("?");
		if (filePathIndex != -1) {
			return path.substring(filePathIndex + 1);
		}
		filePathIndex = path.lastIndexOf("/");
		if (filePathIndex != -1) {
			return path.substring(filePathIndex + 1);
		}
		return null;
	}

	public static String getFileAbsoluteString(String path) {
		int fileStartIndex = path.indexOf("file:");
		if (fileStartIndex != -1) {
			path = path.substring(fileStartIndex + "file:".length());
		}
		if (path.startsWith("/")) {
			int panIndex = path.indexOf(":");
			if (panIndex > 1 && panIndex <= 3) {
				path = path.substring(1);
			}
		}
		return path;
	}

	public static String getFileString(String path) {
		path = getFileAbsoluteString(path);
		int paramIndex = path.lastIndexOf("?");
		if (paramIndex == -1) {
			paramIndex = path.lastIndexOf("!");
		}
		if (paramIndex != -1) {
			path = path.substring(0, paramIndex);
		}
		return path.trim();
	}

	public static String getSuffixString(String path) {
		int suffixIndex = path.lastIndexOf(".");
		if (suffixIndex != -1) {
			return path.substring(suffixIndex + 1);
		}
		return null;
	}

	/**
	 * 说明：创建相对文件路径或class名对应的索引 <br>
	 * 时间：2017年12月28日 下午5:05:44
	 *
	 * @param scanFiles
	 * @return
	 */
	public static Map<String, ScanFile> createFilePathOrClassName$ToScanFileSearchMap(List<ScanFile> scanFiles) {
		Map<String, ScanFile> searchMap = new HashMap<String, ScanFile>();
		for (ScanFile scanFile : scanFiles) {
			String className = scanFile.getClassName();
			String filePath = scanFile.getFilePath();
			searchMap.put(filePath, scanFile);
			if (className != null) {
				searchMap.put(className, scanFile);
			}
		}
		return searchMap;
	}

	public static void updateScanClassInfo(List<ScanFile> scanFiles) {
		for (ScanFile scanFile : scanFiles) {
			String name = scanFile.getClassName();
			if (name != null) {
				try {
					ScanFileInputStream in = new ScanFileInputStream(scanFile);
					ClassFile classFile = ClassReader.read(in);
					ClassInfo classInfo = ClassReader.getClassInfoByClassFile(classFile);
					if (classInfo!= null) {
						ScanClassInfo scanClassInfo = new ScanClassInfo(classInfo);
						scanFile.setScanClassInfo(scanClassInfo);
						scanFile.setClassName(scanClassInfo.getClassName());
					}
				} catch (Exception e) {
					e.printStackTrace();
					System.err.println("处理本地class文件时发生异常:"+ name);
				}
			}
		}
	}

}
